package cn.iocoder.yudao.module.member.controller.admin.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.*;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
import cn.iocoder.yudao.module.member.service.level.MemberLevelService;
import cn.iocoder.yudao.module.member.service.point.MemberPointRecordService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 客户信息")
@RestController
@RequestMapping("/member/user")
@Validated
public class MemberUserController {

    @Resource
    private MemberUserService memberUserService;
    @Resource
    private MemberLevelService memberLevelService;
    @Resource
    private MemberPointRecordService memberPointRecordService;


    @PostMapping("/create")
    @Operation(summary = "创建客户")
    @PermitAll
    //@PreAuthorize("@ss.hasPermission('member:user:create')")
    public CommonResult<MemberUserDO> createUser(@Valid @RequestBody MemberUserRespVO createReqVO) {
        MemberUserDO memberUserDO = memberUserService.createMemberUserIfAbsent(createReqVO);
        return success(memberUserDO);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户")
    @PermitAll
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('crm:member-user:delete')")
    public CommonResult<Boolean> deleteMemberUser(@RequestParam("id") Long id) {
        memberUserService.deleteUser(id);
        return success(true);
    }


    @PutMapping("/update")
    @Operation(summary = "更新客户")
    //@PreAuthorize("@ss.hasPermission('member:user:update')")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody MemberUserUpdateReqVO updateReqVO) {
        memberUserService.updateUser(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-level")
    @Operation(summary = "更新会员用户等级")
    //@PreAuthorize("@ss.hasPermission('member:user:update-level')")
    public CommonResult<Boolean> updateUserLevel(@Valid @RequestBody MemberUserUpdateLevelReqVO updateReqVO) {
        memberLevelService.updateUserLevel(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-point")
    @Operation(summary = "更新会员用户积分")
    @PreAuthorize("@ss.hasPermission('member:user:update-point')")
    public CommonResult<Boolean> updateUserPoint(@Valid @RequestBody MemberUserUpdatePointReqVO updateReqVO) {
        memberPointRecordService.createPointRecord(updateReqVO.getId(), updateReqVO.getPoint(),
                MemberPointBizTypeEnum.ADMIN, String.valueOf(getLoginUserId()));
        return success(true);
    }

    @PutMapping("/update-balance")
    @Operation(summary = "更新会员用户余额")
    @PermitAll
    @PreAuthorize("@ss.hasPermission('member:user:update-balance')")
    public CommonResult<Boolean> updateUserBalance(@Valid @RequestBody Long id) {
        // todo @jason：增加一个【修改余额】
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户详情")
    @PermitAll
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('member:user:query')")
    public CommonResult<MemberUserRespVO> getUser(@RequestParam("id") Long id) {
        MemberUserRespVO user = memberUserService.getMemberUser(id);
        return success(user);
    }

    @GetMapping("/get/dates")
    @Operation(summary = "获得日期数组")
    @PermitAll
    @Parameter(name = "beginTime", description = "开始时间", required = true)
    @Parameter(name = "endTime", description = "结束时间", required = true)
    //@PreAuthorize("@ss.hasPermission('member:user:query')")
    public CommonResult<List<String>> getDates(@RequestParam("beginTime") String beginTime,@RequestParam("endTime") String endTime) {
        List<String> dates = memberUserService.getDates(beginTime,endTime);
        return success(dates);
    }

    @GetMapping("/page")
    //@Operation(summary = "获得客户分页")
    @PermitAll
    //@PreAuthorize("@ss.hasPermission('member:user:query')")
    public CommonResult<PageResult<MemberUserPageResultRespVO>> getUserPage(@Valid MemberUserPageReqVO pageVO) {
        PageResult<MemberUserPageResultRespVO> pageResult = memberUserService.getUserPage(pageVO);
        // 处理用户级别返显
        List<MemberLevelDO> levels = memberLevelService.getLevelList(
                convertSet(pageResult.getList(), MemberUserPageResultRespVO::getLevelId));
        return success(MemberUserConvert.INSTANCE.convertPage(pageResult, levels));
    }

    @GetMapping("/count")
    @Operation(summary = "获得客户账户数量")
    @PermitAll
    //@PreAuthorize("@ss.hasPermission('member:user:query')")
    public CommonResult<MemberInfoCountVO> getUserInfoCount(@Valid CrmMemberUserExportReqVO pageVO) {
        MemberInfoCountVO memberInfoCountVO = memberUserService.getUserInfoCount(pageVO);
        return success(memberInfoCountVO);
    }

}
